package com.chen.web;

import com.chen.dao.GoodsDao;
import com.chen.dao.NewsDao;
import com.chen.dao.impl.GoodsDaoImpl;
import com.chen.dao.impl.NewsDaoImpl;
import com.chen.entity.Goods;
import com.chen.entity.JSONBean;
import com.chen.entity.News;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/index/*")
public class IndexServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String baseURL = request.getContextPath(); //获取项目基准路径
        String uri = request.getRequestURI();//截取项目二级路径
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        JSONBean jsonBean = new JSONBean();
        GoodsDao goodsDao =  new GoodsDaoImpl();
        NewsDao newsDao = new NewsDaoImpl();
        if("goods_list.do".equals(path)){
            List<Goods> goodsList = goodsDao.findPage(1, 3);
            List<News> newsList = newsDao.findPage(1, 4);
            request.setAttribute("newsList",newsList);
            request.setAttribute("goodsList",goodsList);
            request.getRequestDispatcher(baseURL+"/phone/index.jsp").forward(request,response);
        }else if("news_detail.do".equals(path)){
            String id = request.getParameter("id");
            News news = newsDao.findById(id);
            request.setAttribute("news",news);
            request.getRequestDispatcher(baseURL+"/phone/newsdetail.jsp").forward(request,response);
        }else if("newgoods.do".equals(path)){
            List<Goods> goodsList = goodsDao.findAll();
            request.setAttribute("goodsList",goodsList);
            request.getRequestDispatcher(baseURL+"/phone/newgoods.jsp").forward(request,response);
        }else if("specialprice.do".equals(path)){
            List<Goods> goodsList = goodsDao.findAll();
            request.setAttribute("goodsList",goodsList);
            request.getRequestDispatcher(baseURL+"/phone/specialprice.jsp").forward(request,response);
        }else if("reserve.do".equals(path)){
            List<Goods> goodsList = goodsDao.findAll();
            request.setAttribute("goodsList",goodsList);
            request.getRequestDispatcher(baseURL+"/phone/reserve.jsp").forward(request,response);
        }else if("allchips.do".equals(path)){
            List<Goods> goodsList = goodsDao.findPage(1, 5);
            request.setAttribute("goodsList",goodsList);
            request.getRequestDispatcher(baseURL+"/phone/allchips.jsp").forward(request,response);
        }
    }
}
